package main.userInterface.islandui;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;
import java.awt.geom.*;
import java.util.*;

public class SquareSample extends JPanel{
    private static Color SAND = new Color(224,194,64);
    private static Color GRASS = new Color(102,255,109);
    private static Color TREEG = new Color(0,151,8);
    private static Color TREEB = new Color(210,104,0);
    Color skin;
    
    double P = 30.0;//pixels per grid
    private double PG, R, H;
    private int MAX;
    private int HEALTH = 1000;
    private boolean grassy = true;
    private boolean tree = true;
    private boolean critter = true;
    private boolean background = true;
    
    public SquareSample(int TERset, int Pin){
        P = (double)Pin;
        MAX = (int)P;
        resetPixels();
        critter = false;
        switch(TERset){
            case 0: grassy = false; tree = false; break;
            case 1: grassy = true; tree = false; break;
            case 2: grassy = true; tree = true; break;
            case 3: grassy = false; tree = true; background = false; break;
        }
        repaint();
        this.setPreferredSize(new Dimension(MAX,MAX));
    }
    
    public SquareSample(int TERset, boolean CRIset, int Pin, int MAXin){
        P = (double)Pin;
        MAX = MAXin;
        resetPixels();
        switch(TERset){
            case 0: grassy = false; tree = false; break;
            case 1: grassy = true; tree = false; break;
            case 2: grassy = true; tree = true; break;
        }
        critter = CRIset;
        repaint();
        this.setPreferredSize(new Dimension(MAX,MAX));
    }
    
    public void setHealth(int healthIN){
        HEALTH = healthIN;
        resetPixels();
        repaint();
    }

    public void redo(int Pin){
        P=(double)Pin;
        resetPixels();
        repaint();
    }
    
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        Graphics2D g2 =  (Graphics2D)g;
        if(background){
            if(!grassy){g2.setPaint(SAND);}
            else{g2.setPaint(GRASS);}
            g2.fill(new RoundRectangle2D.Double(0,0,P,P,PG,PG));
        }
        if(tree){
            double rx = PG+PG/2-R/2;
            double ry = PG+PG/2-R/2;
            g2.setPaint(TREEB); g2.fill(new Ellipse2D.Double(rx,ry,R,R));
            double angle = 3.6*(100-(HEALTH/10));

            g2.setPaint(TREEG); g2.fill(new Arc2D.Double(rx,ry,R,R,angle,360-angle,Arc2D.PIE));
        }
        if(critter){
            double hy = Math.sqrt((H*H)+((H/2)*(H/2)));         
            double xmod = (PG/2)-(H/2)+(PG*2); 
            double ymod = (PG/2)-(hy/2)+(PG*2);
            int[] polyx = {(int)(xmod), (int)(H+xmod), (int)((H/2)+xmod)};
            int[] polyy = {(int)(ymod), (int)(ymod),   (int)(hy+ymod)};
            g2.setPaint(skin);
            g2.fill(new Polygon(polyx,polyy,3));
        }
    }
    
    private void setSkinColor(){
        int valueCol = r(300);
        setSkinColor(valueCol,300);
    }
    
    private void setSkinColor(int valueCol,int maxCol){
        int[][] codec = {{0,1,1,1,-1},{0,0,1,0,1},{1,0,1,2,-1},{1,0,0,1,1}}; //color codes for setting critters
        double per = (double)valueCol/((double)maxCol/codec.length);
        int fper = (int)Math.floor(per);
        int[] rgb = {255*codec[fper][0],255*codec[fper][1],255*codec[fper][2]};
        int modifier = (int)(255*(per-fper)*codec[fper][4]);
        rgb[codec[fper][3]] = rgb[codec[fper][3]]+modifier;
        int darken = 15;
        if(rgb[0]>darken){rgb[0]=rgb[0]-darken;}
        if(rgb[1]>darken){rgb[1]=rgb[1]-darken;}
        if(rgb[2]>darken){rgb[2]=rgb[2]-darken;}
        skin = new Color(rgb[0],rgb[1],rgb[2]);
    }
    
    private void resetPixels(){
        PG = P/3.0;
        R = (P/9)*3.0;
        H = (P/10)*2.0;
        if(!background){
            R=P;
        }
    }
    
    private int r(int range){
        Random randomGenerator = new Random();
        int x = randomGenerator.nextInt(range);
        return x;
    }
}